class TodoListsController < ApplicationController
  layout "application"

  def index
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
    @todo_list_pages, @todo_lists = paginate :todo_lists, :per_page => 25
  end

  def show
    @todo_list = TodoList.find(params[:id])
    @deliverables = @todo_list.deliverables
    @deliverables = Deliverable.sort_deliverables(@deliverables)
  end

  def new
    @todo_list = TodoList.new
  end

  def create
    @todo_list = TodoList.new(params[:todo_list])
    if @todo_list.save
      flash[:notice] = 'TodoList was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end

  def edit
    @todo_list = TodoList.find(params[:id])
  end

  def update
    @todo_list = TodoList.find(params[:id])
    if @todo_list.update_attributes(params[:todo_list])
      flash[:notice] = 'TodoList was successfully updated.'
      redirect_to :action => 'show', :id => @todo_list
    else
      render :action => 'edit'
    end
  end

  def destroy
    TodoList.find(params[:id]).destroy
    redirect_to :action => 'list'
  end

  def move_to_most_recent_iteration

    deliverableIds = params[:deliverableIds]

    if deliverableIds != nil
      most_recent_iteration = Iteration.most_recent_iteration

      for deliverableId in deliverableIds
        deliverable = Deliverable.find_by_id(deliverableId)
        deliverable.schedulable = most_recent_iteration
        deliverable.status = nil
        deliverable.save
      end

      flash[:notice] = 'Successfully moved deliverables to current iteration'
    else
      flash[:notice] = 'Please select deliverable(s) to move'
    end
    redirect_to :action => 'show', :id => params[:todo_list_id]
  end
end
